<template>
  <a-form
    :model="formState"
    ref="formDataRef"
    name="basic"
    :label-col="{ style: 'width:90px' }"
    :wrapper-col="{ style: 'calc(100% - 90px)' }"
    autocomplete="off"
    @finish="onFinish"
    @finishFailed="onFinishFailed"
  >
    <a-form-item label="费用名称" name="costName" :rules="[{ required: true, message: '请选择费用名称!' }]">
      <a-select v-model:value="formState.costName" placeholder="请选择" :filter-option="filterOption" show-search :disabled="type === 2">
        <a-select-option v-for="(item, index) in dataDict['FEE_NAME']" :key="index" :value="item.name">{{ item.name }}</a-select-option>
      </a-select>
    </a-form-item>
    <a-form-item label="单价" name="unitPrice" :rules="[{ required: true, message: '请填写单价!' }]">
      <a-input-number v-model:value="formState.unitPrice" :min="-9999.99" :max="999999.99" :precision="2" placeholder="请输入" />
    </a-form-item>
    <a-form-item label="单位" name="unit" :rules="[{ required: true, message: '请选择单位!' }]">
      <a-select v-model:value="formState.unit" placeholder="请选择">
        <a-select-option value="票">票</a-select-option>
        <a-select-option value="件">件</a-select-option>
        <a-select-option value="KG">KG</a-select-option>
      </a-select>
    </a-form-item>

    <a-form-item :wrapper-col="{ offset: 8, span: 16 }" class="submit-btn-form-item">
      <a-button :loading="loading" type="primary" @click="submitHandle">确定</a-button>
      <a-button @click="resetHandle">取消</a-button>
    </a-form-item>
  </a-form>
</template>

<script lang="ts" setup>
import { toRefs, watch } from "vue";
import CommonHook from "@/hook/commonHook";
import InitHook from "./composables/initHook";
import OperationHook from "./composables/operationHook";
const props = withDefaults(
  defineProps<{
    visible: boolean;
    passData: Base.GeneraDataType;
    type: number; //1新增 ，2编辑
  }>(),
  {}
);
const emit = defineEmits<{
  (e: "update:visible", blo: boolean): void;
  (e: "addChargeSubmit"): void;
}>();
const { dataDict } = CommonHook();
const { visible, passData, type } = toRefs(props);
const { formDataRef, formState, loading } = InitHook();
const { onFinish, onFinishFailed, resetFields, resetHandle, submitHandle, filterOption } = OperationHook({ formDataRef, formState, passData, emit, loading, type });

const init = () => {
  console.log(type.value);
  if (type.value === 2) {
    const { costName, unitPrice, unit } = passData.value;

    formState.costName = costName;
    formState.unitPrice = unitPrice;
    formState.unit = unit;
  }
};

watch(
  () => visible.value,
  (v: boolean) => {
    if (!v) {
      resetFields();
      formDataRef.value.clearValidate();
    } else {
      init();
    }
  }
);
init();
</script>
<style lang="stylus" scoped>
:deep(.ant-input-number){
    width: 100%;
}
</style>
